package cn.easybuy.servlet.product;

import cn.easybuy.entity.Product;
import cn.easybuy.entity.ProductCategory;
import cn.easybuy.service.ProductService;
import cn.easybuy.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/productCate2Servlet")
public class ProductCate2Servlet extends HttpServlet {
    private ProductService productService = new ProductServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            int id = Integer.parseInt(req.getParameter("id"));
            int type = Integer.parseInt(req.getParameter("type"));
            List<Product> productList = productService.getProListByCate2(id,type);
            req.setAttribute("productList",productList);

            List<ProductCategory> listProCate = productService.getALlCate();
            req.setAttribute("productCategoryVoList",listProCate);
            //查询分类下的所有商品列表
            for (ProductCategory category : listProCate) {
                List<Product> proDuctList = productService.getProDuctList(category.getId());
                category.setProductList(proDuctList);
            }
            req.getRequestDispatcher("pre/product/queryProductList.jsp").forward(req,resp);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
